﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using DataModel;


namespace BasicDataManagement
{
    /// <summary>
    /// AddReplaceableDishItemWindow.xaml 的交互逻辑
    /// </summary>
    public partial class AddReplaceableDishItemWindow : Window
    {
        public ObservableCollection<string> Cats { get; set; }
        public string DefaultCat { get; set; }

        public ObservableCollection<Ingredient> MaterialList { get; set; }
        public ObservableCollection<Ingredient> OptionList { get; set; }

        public ReplaceableDishObject rDishObject { get; set; }

        IOpertation op = OperationFactory.Create();

        public AddReplaceableDishItemWindow()
        {
            InitializeComponent();
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            if (rDishObject == null)
            {
                rDishObject = new ReplaceableDishObject();
            }

            MaterialList = new ObservableCollection<Ingredient>(rDishObject.MaterialList);
            OptionList = new ObservableCollection<Ingredient>(rDishObject.OptionList);

            listViewMaterial.ItemsSource = MaterialList;
            listViewOption.ItemsSource = OptionList;
            
            catListCombo.ItemsSource = Cats;
            catListCombo.SelectedIndex = Cats.IndexOf(rDishObject.BelongedCategoryName);

            priceTB.Text = rDishObject.Price.ToString();

            unitTB.Text = rDishObject.Unit;
            nameTB.Text = rDishObject.ObjectName;
        }

        private void btnAddMaterial_Click(object sender, RoutedEventArgs e)
        {
            //AddIngredientWindow win = new AddIngredientWindow();
            //win.Cats = new ObservableCollection<string>(op.LoadMateriaclItemName());
            //win.Owner = this;
            //win.WindowStartupLocation = WindowStartupLocation.CenterOwner;
            //if (win.ShowDialog() == true)
            //{
            //    MaterialList.Add(win.IngredientResult);
            //}
            AddSubItemWindow win = new AddSubItemWindow();
            win.ingredientList = op.LoadMaterialSimpleObjects();
            win.Owner = this;
            win.WindowStartupLocation = WindowStartupLocation.CenterOwner;
            if (win.ShowDialog() == true)
            {
                MaterialList.Add(win.IngredientResult);
            }
        }


        private void btnDelMaterial_Click(object sender, RoutedEventArgs e)
        {
            if (listViewMaterial.SelectedItem != null)
            {
                MaterialList.Remove(listViewMaterial.SelectedItem as Ingredient);
                listViewMaterial.SelectedItem = null;
            }
        }

        private void btnAddOption_Click(object sender, RoutedEventArgs e)
        {
            //AddIngredientWindow win = new AddIngredientWindow();
            //win.Cats = new ObservableCollection<string>(op.LoadOptionItemName());
            //win.Owner = this;
            //win.WindowStartupLocation = WindowStartupLocation.CenterOwner;
            //if (win.ShowDialog() == true)
            //{
            //    OptionList.Add(win.IngredientResult);
            //}
            AddSubItemWindow win = new AddSubItemWindow();
            win.ingredientList = op.LoadOptionSimpleObjects();
            win.Owner = this;
            win.WindowStartupLocation = WindowStartupLocation.CenterOwner;
            if (win.ShowDialog() == true)
            {
                OptionList.Add(win.IngredientResult);
            }
        }

        private void btnDelOption_Click(object sender, RoutedEventArgs e)
        {
            if (listViewOption.SelectedItem != null)
            {
                OptionList.Remove(listViewOption.SelectedItem as Ingredient);
                listViewOption.SelectedItem = null;
            }
        }

        private void btnOK_Click(object sender, RoutedEventArgs e)
        {
            rDishObject.ObjectName = nameTB.Text;
            rDishObject.Unit = unitTB.Text;
            rDishObject.BelongedCategoryName = catListCombo.Text;
            rDishObject.MaterialList = MaterialList.ToList();
            rDishObject.OptionList = OptionList.ToList();
            try
            {
                rDishObject.Price = Convert.ToDecimal(priceTB.Text);
            }
            catch (Exception)
            {
                MessageBox.Show("售价格式有误，请重新输入");
                return;
            }
            this.DialogResult = true;
            this.Close();
        }

        private void btnCancel_Click(object sender, RoutedEventArgs e)
        {
            this.DialogResult = false;
            this.Close();
        }

    }
}
